package com.youlu.campus.wechat.service.impl;

import com.youlu.campus.wechat.entity.WechatAppIdAccount;
import com.youlu.campus.wechat.service.WechatAppIdAccountService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: WechatAppIdAccountServiceImpl
 * @Date 2025/10/27
 * @module WechatAppIdAccountServiceImpl
 * @author: su
 */
@Slf4j
@Service
public class WechatAppIdAccountServiceImpl implements WechatAppIdAccountService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public WechatAppIdAccount save(WechatAppIdAccount account) {
        LocalDateTime now = LocalDateTime.now();
        if (StringUtils.isEmpty(account.getId())) {
            account.setCreateTime(now);
        }
        account.setUpdateTime(now);
        return mongoTemplate.save(account);
    }

    @Override
    public List<WechatAppIdAccount> listByMerchantId(String merchantId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("merchantId").is(merchantId));
        return mongoTemplate.find(query, WechatAppIdAccount.class);
    }

    @Override
    public WechatAppIdAccount findFirst() {
         return mongoTemplate.findOne(new Query(), WechatAppIdAccount.class);
    }

    @Override
    public void deleteById(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        mongoTemplate.remove(query, WechatAppIdAccount.class);
    }

    @Override
    public WechatAppIdAccount findById(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, WechatAppIdAccount.class);
    }
}
